//
//  HYHud.m
//  HaiYue
//
//  Created by skyline on 2018/6/20.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYHud.h"
@interface HYHud()
{
    UIWindow *_window;
}
@property (nonatomic, strong) UIActivityIndicatorView *hudView;
@property (nonatomic, assign) UIActivityIndicatorViewStyle style;
@end
@implementation HYHud

- (instancetype)initWithHudStyle:(UIActivityIndicatorViewStyle)style
{
    self = [super init];
    if (self)
    {
        _style = style;
        self.bounds = CGRectMake(0, 0, 100, 100);
        self.layer.cornerRadius = 5.0;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0.5;
        self.windowLevel = UIWindowLevelAlert;
        self.translatesAutoresizingMaskIntoConstraints = NO;

        [self makeKeyAndVisible];

//        self.autoresizesSubviews = YES;
    }
    return self;
}
static HYHud *_hyhud;
static dispatch_once_t once;
+ (instancetype)hud
{
    if (!_hyhud)
    {
        dispatch_once(&once, ^{
            _hyhud = [[HYHud alloc]initWithHudStyle:0];
        });
    }
    return _hyhud;
}

- (UIActivityIndicatorView *)hudView
{
    if (!_hudView)
    {
        _hudView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:_style];
        _hudView.bounds = CGRectMake(0, 0, self.bounds.size.width / 2, self.bounds.size.width / 2);
        _hudView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_hudView];
        //        [_hudView mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.center.equalTo(self);
        //        }];
        [self setupLayout];
        
    }
    return _hudView;
}

- (void)setupLayout
{
    NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:_hudView attribute:NSLayoutAttributeCenterX
                                                               relatedBy:NSLayoutRelationEqual toItem:self
                                                               attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    
    NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:_hudView attribute:NSLayoutAttributeCenterY
                                                               relatedBy:NSLayoutRelationEqual toItem:self
                                                               attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    //
        NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:_hudView attribute:NSLayoutAttributeWidth
                                                                 relatedBy:NSLayoutRelationEqual toItem:nil
                                                                 attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50];

        NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:_hudView attribute:NSLayoutAttributeHeight
                                                                  relatedBy:NSLayoutRelationEqual toItem:nil
                                                                  attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50];


    [_hudView addConstraints:@[width, height]];
    [self addConstraints:@[centerX,centerY]];
    NSLayoutConstraint *w = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth
                                                         relatedBy:NSLayoutRelationEqual toItem:nil
                                                         attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100];
    
    NSLayoutConstraint *h = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight
                                                         relatedBy:NSLayoutRelationEqual toItem:nil
                                                         attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100];
    [self addConstraints:@[w,h]];

}

- (void)setColor:(UIColor *)color
{
    if (_color != color)
    {
        _color = color;
        self.hudView.color = color;
    }
}

- (void)setTimes:(CGFloat)times
{
    if (_times != times)
    {
        _times = times;
        
//        if (self.hudView.bounds.size.width * times <= self.bounds.size.width )
//        {
//            self.hudView.transform = CGAffineTransformMakeScale(times, times);
//        }
    }
}

- (void)startAnimating
{
    self.hidden = NO;
    //    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    
    
    //    UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;
    //    UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;
    //    UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar
    
    //    _window =[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //    _window.backgroundColor = [UIColor redColor];
    //    _window.windowLevel = UIWindowLevelAlert;
    //    [_window addSubview:self];
    //    [self mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.center.equalTo(_window);
    //        make.size.mas_equalTo(self.bounds.size);
    //    }];
    [self.hudView startAnimating];
    //    [_window makeKeyAndVisible];
    
}

- (void)stopAnimating
{
    self.hidden = YES;
    [self removeFromSuperview];
    [self.hudView stopAnimating];
}
@end
